﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      CCS Send Email Windows Services
// Modify By:      Leo Ren (leoren@wicresoft.com)
// Modify Date:    2011-2-13
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Text;
using System.Xml;
using BEGDC.CC.BL.CCFlow.EV;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;
using BEGDC.Utility.ExceptionManagement;
using Castle.Facilities.NHibernateIntegration;
using CCEmailService;

namespace APEmailAlertService
{
    public partial class Service1 : ServiceBase
    {
        private ISessionManager sessionManager;
        private System.Timers.Timer timer = new System.Timers.Timer(3000);

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            //timer.Interval = 60 * 60 * 1000;
            timer.Interval = 3 * 1000;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.AutoReset = true;
            timer.Enabled = true;
            timer.Start();
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ////Send Email at 8:00
            //if (System.DateTime.Now.Hour == 8)
            //{
            try
            {
                IEV ievService = AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.EVService] as IEV;
                this.sessionManager = (ievService as EVService).sessionManager;
                if (sessionManager == null)
                {
                    Log("sessionManager is null");
                }
                //UnClosed Action Point
                IList unClosedAPList = ievService.GetUnClosedAP();
                if (unClosedAPList != null && unClosedAPList.Count > 0)
                {
                    for (int i = 0; i < unClosedAPList.Count; i++)
                    {
                        BEGDC.CC.Entity.ActionPoint ap = unClosedAPList[i] as BEGDC.CC.Entity.ActionPoint;
                        this.SendEmailsOfUnClosedAP(ap, ievService);
                    }
                }
            }
            catch (Exception ex)
            {
                Log(ex.ToString());
            }
            // }
        }

        protected override void OnStop()
        {
            timer.Stop();
        }

        private void SendEmailsOfUnClosedAP(BEGDC.CC.Entity.ActionPoint ap, IEV ievService)
        {
            string from = "asbcc@alcatel-sbell.com.cn";
            string toEmails = string.Empty;
            string cc = string.Empty;
            string subject = string.Empty;
            string body = string.Empty;

            //TODO:Send Emails
            EmailService email = new EmailService(this.sessionManager);
            //Get receivers' Email
            //toEmails += ievService.GetEmailsOfUnClosedAPoint(ap);
            toEmails = "leoren@wicresoft.com";
            subject = "a";
            body = "aaa";

            this.Log("Start Send Email: \r\n");

            if (!email.SendMail(from, "leoren@wicresoft.com", cc, subject, body))
            {
                Log(ap, toEmails);
            }

            //MailUtility.SendMail(toEmails, cc, subject, body);
        }

        private void Log(BEGDC.CC.Entity.ActionPoint ap, string toEmails)
        {
            //System.Environment.CurrentDirectory
            FileStream file;

            if (!File.Exists("C:\\SendAPEmialsLogMsg.txt"))
            {
                file = new FileStream("C:\\SendAPEmialsLogMsg.txt", FileMode.Create, FileAccess.Write);
            }
            else
            {
                file = System.IO.File.Open(@"C:\SendAPEmialsLogMsg.txt", System.IO.FileMode.Append);
            }

            System.Console.Write(DateTime.Now);
            StringBuilder sb = new StringBuilder();
            sb.Append("============================================\r\n");
            sb.AppendFormat("Sended Email failed--Action Point No:{0}\r\n--To:{1}\r\n--At:{2}\r\n", ap.APNo, toEmails, DateTime.Now);
            StreamWriter sw = new StreamWriter(file);
            sw.Write(sb.ToString());
            sw.Close();
            file.Close();
        }

        private void Log(string logMsg)
        {
            FileStream file;

            if (!File.Exists("C:\\SendAPEmialsLogMsg.txt"))
            {
                file = new FileStream("C:\\SendAPEmialsLogMsg.txt", FileMode.Create, FileAccess.Write);
            }
            else
            {
                file = System.IO.File.Open(@"C:\SendAPEmialsLogMsg.txt", System.IO.FileMode.Append);
            }

            System.Console.Write(DateTime.Now);
            StringBuilder sb = new StringBuilder();
            sb.Append("============================================\r\n");
            sb.AppendFormat("Exception at {0}:{1}\r\n", DateTime.Now, logMsg);
            StreamWriter sw = new StreamWriter(file);
            sw.Write(sb.ToString());
            sw.Close();
            file.Close();
        }
    }
}
